#!/bin/bash

echo "========================================"
echo "Markdown智能排版 Word 加载项启动脚本"
echo "========================================"
echo

# 检查是否安装了Node.js
if ! command -v node &> /dev/null; then
    echo "错误: 未找到Node.js，请先安装Node.js"
    echo "下载地址: https://nodejs.org/"
    exit 1
fi

# 检查是否安装了依赖
if [ ! -d "node_modules" ]; then
    echo "正在安装依赖..."
    npm install
    echo
fi

# 显示选项
echo "请选择启动方式:"
echo "1. 启动开发服务器 (http-server)"
echo "2. 启动开发服务器并打开浏览器"
echo "3. 使用Python启动服务器"
echo "4. 验证manifest.xml文件"
echo "5. 打开测试页面"
echo

read -p "请输入选项 (1-5): " choice

case $choice in
    1)
        echo "启动开发服务器 (端口3000)..."
        npm start
        ;;
    2)
        echo "启动开发服务器并打开浏览器..."
        npm run dev
        ;;
    3)
        echo "使用Python启动服务器..."
        python3 -m http.server 3000 || python -m http.server 3000
        ;;
    4)
        echo "验证manifest.xml文件..."
        npm run validate
        ;;
    5)
        echo "启动服务器并打开测试页面..."
        if command -v open &> /dev/null; then
            open http://localhost:3000/test.html
        elif command -v xdg-open &> /dev/null; then
            xdg-open http://localhost:3000/test.html
        fi
        npm start
        ;;
    *)
        echo "无效选项，默认启动开发服务器..."
        npm start
        ;;
esac
